package cn.zn.modules.zzsys.cur.controller;

import cn.zn.framework.common.abs.AbstractController;
import cn.zn.framework.common.utils.PageUtils;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.zzsys.cur.service.TTeacherInfoService;
import cn.zn.modules.zzsys.cur.entity.TTeacherInfo;
import cn.zn.modules.zzsys.cur.entity.dto.TeacherInfoListDTO;
import cn.zn.modules.zzsys.enums.UserImgTypeEnum;
import cn.zn.modules.zzsys.inter.TeacherInter;
import cn.zn.modules.zzsys.inter.vo.TeacherInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

@RestController
@RequestMapping("zzsys/teacher")
@Api(tags = "教职工管理接口")
public class TTeacherInfoController extends AbstractController {
    @Autowired
    private TTeacherInfoService tTeacherInfoService;
    @Autowired
    private TeacherInter teacherInter;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("教职工列表")
    public R list(@RequestBody TeacherInfoListDTO dto) {

        PageUtils pageUtils = tTeacherInfoService.queryPage(dto);

        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        TTeacherInfo tTeacherInfo = tTeacherInfoService.getById(id);

        return R.ok().put("teacher", tTeacherInfo);
    }

    /**
     * 当前用户信息
     *
     * @return
     */
    @RequestMapping("/self")
    public R self() {
        TeacherInfoVO teacherInfoVO = teacherInter.findByWorkNo(super.loginUser().getUsername());

        return R.ok().put("teacher", teacherInfoVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TTeacherInfo tTeacherInfo) {

        tTeacherInfoService.save(tTeacherInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TTeacherInfo tTeacherInfo) {

        tTeacherInfoService.updateById(tTeacherInfo);

        return R.ok();
    }

    /**
     * 保存签意
     *
     * @param teacherId
     * @param fileupload
     * @return
     */
    @PostMapping("/upload/sign/{teacherId}")
    public R uploadSign(@PathVariable String teacherId, @RequestParam("fileupload") MultipartFile[] fileupload) {
        return R.ok().put("path", tTeacherInfoService.saveImgs(UserImgTypeEnum.SIGN, teacherId, fileupload[0]));
    }

    /**
     * 保存同意
     *
     * @param teacherId
     * @param fileupload
     * @return
     */
    @PostMapping("/upload/agree/{teacherId}")
    public R uploadAgree(@PathVariable String teacherId, @RequestParam("fileupload") MultipartFile[] fileupload) {
        return R.ok().put("path", tTeacherInfoService.saveImgs(UserImgTypeEnum.AGREE, teacherId, fileupload[0]));
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        tTeacherInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation("教职工信息导入")
    @PostMapping("/importDetail")
    public R importDetail(@RequestParam("file") MultipartFile file) {
        tTeacherInfoService.importDetail(file);
        return R.ok();
    }

    @ApiOperation("教职工信息导入保存")
    @PostMapping("/saveAll")
    public R saveAll() {
        tTeacherInfoService.saveAll();
        return R.ok();
    }

    @ApiOperation("字模文件工号匹配")
    @PostMapping("/buildSign")
    public R buildSign() {
        tTeacherInfoService.buildSign();
        return R.ok();
    }
}
